<?php
class editproject extends controller{
	public $project;
	public $customers;
	public $workers;
	public $developers;

	public function __construct($url){
		parent::__construct($url, true);
		$this->project = null;
		$this->customers = null;
		$this->workers = null;
		$this->developers = null;
	}

	public function check(){
		if($_SESSION["user"][5] < 3) $this->redirect("start", "Nincs jogosultsága ehhez a funkcióhoz!");
		if(!isset($this->url[1]) || empty($this->url[1])) $this->redirect("start", "A megadott projekt nem létezik!");
	}

	public function run(){
		$this->getProject();
		$this->getCustomers();
		$this->getWorkers();
		$this->getDevelopers();

		$this->editproject();
		$this->adddeveloper();
		$this->deldeveloper();
	}

	private function getProject(){
		$sql = "SELECT name, descr, customer, status FROM project WHERE id=".$this->url[1];
		$tmp = $this->database->fetchRow($sql);
		$this->project = $tmp[0];
	}

	private function getCustomers(){
		$sql = "SELECT id, name FROM user WHERE status > 0 AND status <> 2";
		$this->customers = $this->database->fetchRow($sql);
	}

	private function getWorkers(){
		$sql = "SELECT developer.id, user.name FROM user, developer WHERE user.id = developer.developer AND developer.project_id=".$this->url[1];
		$this->workers = $this->database->fetchRow($sql);
	}

	private function getDevelopers(){
		$sql = "SELECT user.id, user.name FROM user WHERE status = 2 AND id NOT IN (SELECT developer FROM developer WHERE project_id = ".$this->url[1].")";
		$this->developers = $this->database->fetchRow($sql);
	}

	private function editproject(){
		if(isset($_POST["editproject"])){
			if(!empty($_POST["name"]) && !empty($_POST["descr"]) && !empty($_POST["customer"]) && !empty($_POST["status"])){
				$sql = "UPDATE project SET name='".$_POST["name"]."', descr='".$_POST["descr"]."', customer='".$_POST["customer"]."', status='".($_POST["status"]-1)."' WHERE id=".$this->url[1];
				if($this->database->query($sql)){
					$this->redirect($this->self, "A projekt módosítása sikeres volt.");
				}
				else{
					$this->message = "Adatbázis hiba történt a projekt szerkesztése közben!";
				}
			}
			else{
				$this->message = "Üresen hagyott mező!";
			}
		}
	}

	private function adddeveloper(){
		if(isset($_POST["adddeveloper"])){
			if(!empty($_POST["developer"])){
				$sql = "INSERT INTO developer(project_id, developer) VALUES(".$this->url[1].", ".$_POST["developer"].")";
				if($this->database->query($sql)){
					$this->redirect($this->self, "A projekt módosítása sikeres volt.");
				}
				else{
					$this->message = "Adatbázis hiba történt a projekt szerkesztése közben!";
				}
			}
			else{
				$this->message = "Üresen hagyott mező!";
			}
		}
	}

	private function deldeveloper(){
		if(isset($_POST["deldeveloper"])){
			if(!empty($_POST["developer"])){
				$sql = "DELETE FROM developer WHERE id=".$_POST["developer"];
				if($this->database->query($sql)){
					$this->redirect($this->self, "A projekt módosítása sikeres volt.");
				}
				else{
					$this->message = "Adatbázis hiba történt a projekt szerkesztése közben!";
				}
			}
			else{
				$this->message = "Üresen hagyott mező!";
			}
		}
	}
}
?>
